﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Models.ScreenClient;
using qltscd.Contansts;
using Telerik.Web.Mvc;
using qltscd.Helper;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;

namespace qltscd.Controllers
{ 
    public class LuanChuyenController : BaseController
    {
        #region Properties Section
        private QLTBEntities db = new QLTBEntities();
        #endregion

        #region Views Section
        public ActionResult Create()
        {
            ViewBag.DonViGiao = new SelectList(db.DanhMucDonViQuanLies, "IdentityDonVi", "TenDonVi");
            ViewBag.DonViNhan = new SelectList(db.DanhMucDonViQuanLies, "IdentityDonVi", "TenDonVi");
            ViewBag.ThietBiLuanChuyen = new SelectList(db.DanhMucThietBis, "IdentityThietBi", "SerialThietBi");
            Models.ScreenClient.LuanChuyenScreenClient screen = new LuanChuyenScreenClient();
            return View(screen);
        }
        [GridAction]
        public ActionResult _Create () {
            LuanChuyenScreenClient screen = (LuanChuyenScreenClient)Session[SessionScreenContansts.LuanChuyenScreenClient];
            screen = Seacrch(screen);
            return View(new GridModel(screen.DSLuanChuyen));
        }
        #endregion

        #region CRUD Section
        [HttpPost]
        public ActionResult Create(Models.ScreenClient.LuanChuyenScreenClient luanchuyenthietbi)
        {
            luanchuyenthietbi.DSLuanChuyen = ((LuanChuyenScreenClient)Session[SessionScreenContansts.LuanChuyenScreenClient]).DSLuanChuyen;
            if (luanchuyenthietbi.DSLuanChuyen.Count == 0) {
                ViewBag.DonViGiao = new SelectList(db.DanhMucDonViQuanLies, "IdentityDonVi", "TenDonVi", luanchuyenthietbi.DonViGiao);
                ViewBag.DonViNhan = new SelectList(db.DanhMucDonViQuanLies, "IdentityDonVi", "TenDonVi", luanchuyenthietbi.DonViNhan);
                ViewBag.ThietBiLuanChuyen = new SelectList(db.DanhMucThietBis, "IdentityThietBi", "SerialThietBi");
                return View(luanchuyenthietbi);
            }
            if (ModelState.IsValid)
            {
                LuanChuyenThietBi lc = new LuanChuyenThietBi();
                if (db.LuanChuyenThietBis.Count(o => o.MaLuanChuyen == luanchuyenthietbi.MaLuanChuyen) > 0)
                {
                    ViewData["erMaLuanChuyen"] = "Mã luân chuyển " + luanchuyenthietbi.MaLuanChuyen + " đã tồn tại";
                    ViewBag.DonViGiao = new SelectList(db.DanhMucDonViQuanLies, "IdentityDonVi", "TenDonVi", luanchuyenthietbi.DonViGiao);
                    ViewBag.DonViNhan = new SelectList(db.DanhMucDonViQuanLies, "IdentityDonVi", "TenDonVi", luanchuyenthietbi.DonViNhan);
                    ViewBag.ThietBiLuanChuyen = new SelectList(db.DanhMucThietBis, "IdentityThietBi", "SerialThietBi");
                    return View(luanchuyenthietbi);
                }
                //save phieu luan chuyen
                ObjectHelper.Copy(luanchuyenthietbi, lc);
                db.LuanChuyenThietBis.AddObject(lc);
                db.SaveChanges();

                //save chi tiet luan chuyen
                foreach (Models.BaseClient.ChiTietLuanChuyen chitiet in luanchuyenthietbi.DSLuanChuyen) {
                    LuanChuyenThietBi luanchuyen = db.LuanChuyenThietBis.OrderByDescending(i => i.IdentityLuanChuyen).First();
                    ChiTietLuanChuyen ct = new ChiTietLuanChuyen();
                    ct.IdentityLuanChuyen = luanchuyen.IdentityLuanChuyen;
                    ct.IdentityThietBi = chitiet.IdentityThietBi;
                    db.ChiTietLuanChuyens.AddObject(ct);
                }
                db.SaveChanges();
                Session[SessionScreenContansts.LuanChuyenScreenClient] = null;
                return RedirectToAction("Create");
            }

            ViewBag.DonViGiao = new SelectList(db.DanhMucDonViQuanLies, "IdentityDonVi", "TenDonVi", luanchuyenthietbi.DonViGiao);
            ViewBag.DonViNhan = new SelectList(db.DanhMucDonViQuanLies, "IdentityDonVi", "TenDonVi", luanchuyenthietbi.DonViNhan);
            ViewBag.ThietBiLuanChuyen = new SelectList(db.DanhMucThietBis, "IdentityThietBi", "SerialThietBi");
            return View(luanchuyenthietbi);
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Update (int id) {
            LuanChuyenScreenClient screen = new LuanChuyenScreenClient();
            screen = Seacrch(screen);
            Models.BaseClient.ChiTietLuanChuyen chitiet = screen.DSLuanChuyen.Single(i => i.IdentityChiTietLuanChuyen == id);
            TryUpdateModel(chitiet);
            Session[SessionScreenContansts.LuanChuyenScreenClient] = screen;
            return View(new GridModel(screen.DSLuanChuyen));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Insert () {
            LuanChuyenScreenClient screen = new LuanChuyenScreenClient();
            screen = Seacrch(screen);
            Models.BaseClient.ChiTietLuanChuyen chitiet = new Models.BaseClient.ChiTietLuanChuyen();
            if (TryUpdateModel(chitiet)) {
                chitiet.IdentityChiTietLuanChuyen = -screen.DSLuanChuyen.Count - 1;
                screen.DSLuanChuyen.Add(chitiet);
            }
            screen = Seacrch(screen);
            Session[SessionScreenContansts.LuanChuyenScreenClient] = screen;
            return View(new GridModel(screen.DSLuanChuyen));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Delete (int id) {
            LuanChuyenScreenClient screen = new LuanChuyenScreenClient();
            screen = Seacrch(screen);
            Models.BaseClient.ChiTietLuanChuyen del = screen.DSLuanChuyen.Single(i => i.IdentityChiTietLuanChuyen == id);
            screen.DSLuanChuyen.Remove(del);
            Session[SessionScreenContansts.LuanChuyenScreenClient] = screen;
            return View(new GridModel(screen.DSLuanChuyen));
        }
        #endregion

        #region Helper Section
        private LuanChuyenScreenClient Seacrch (LuanChuyenScreenClient screen) {
            if (screen == null) {
                screen = new LuanChuyenScreenClient();
            }
            if (Session[SessionScreenContansts.LuanChuyenScreenClient] != null) {
                screen = (LuanChuyenScreenClient)Session[SessionScreenContansts.LuanChuyenScreenClient];
            }
            screen.DSLuanChuyen = screen.DSLuanChuyen.OrderBy(i => i.SerialThietBi).ToList();
            foreach (Models.BaseClient.ChiTietLuanChuyen chitiet in screen.DSLuanChuyen) {
                DanhMucThietBi thietbi = db.DanhMucThietBis.Single(i=>i.IdentityThietBi == chitiet.IdentityThietBi);
                chitiet.MaThietBi = thietbi.MaThietBi;
                chitiet.TenThietBi = thietbi.TenThietBi;
                chitiet.TheThietBi = thietbi.TheThietBi;
                chitiet.SerialThietBi = thietbi.SerialThietBi;
            }
            return screen;
        }
        #endregion
    }
}